<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Goods;
use Illuminate\Http\Request;

use App\Http\Controllers\Admin\CommonController;
use App\Http\Model\Category;
use Illuminate\Support\Facades\Input;
class CategoryController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list = \DB::table("category")->get();
        $list = $this->findSubTree($list,0,0);
//        给模板传变量的三种方

        return  view('admin.category.index',compact('list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        return view('admin.category.add');
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

       $input = Input::except('_token');
       $input['created_at'] = date('Y-m-d h:i:s',time());
       $re = Category::create($input);

        if($re){
            return redirect('admin/category');
        }else{
            return back()->with('msg','分类添加失败')->withInput();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $list = \DB::table("category")->get();
        $list = $this->findSubTree($list,0,0);
        $data = Category::find($id);
        return view('admin.category.addzi',compact("list","data"));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cate_one = Category::where('cate_pid',0)->get();
        $cate_one = $this->findSubTree($cate_one,0,0);
        $data = Category::find($id);
        return view('admin.category.edit',compact('data','cate_one'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = Input::except('_token','_method');
        $cate = Category::find($id);
        $re = $cate->update($input);
        if($re){
            return redirect('admin/category');
        }else{
            return back()->with('errors','分类修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $cate = Category::get();
        foreach($cate as $list){
            $cate_pid = $list->cate_pid;
        }
        $goods = Goods::get();
        foreach($goods as $list){
            $goods_id = $list->cate_id;
        }
        if($id != $cate_pid & $id !=$goods_id) {

            $re = Category::where('cate_id', $id)->delete();
            if ($re) {
                $data = [
                    'status' => 0,
                    'msg' => '类别删除成功'
                ];
            } else {
                $data = [
                    'status' => 1,
                    'msg' => '类别删除失败'
                ];
            }
        }else{
            return "此类别不可以删除！";
        }
        return $data;
    }
    protected function findSubTree($cates,$cate_id=0,$level=1){
        $subtree = [];//子孙数组
        foreach ($cates as $v) {
            if($v->cate_pid==$cate_id){
                $v->level = $level;
                $subtree[] = $v;
                $subtree = array_merge($subtree,$this->findSubTree($cates,$v->cate_id,$level+1));
            }
        }
        return $subtree;
    }
}
